﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.model;
using InHD.utilities;
using System.Data;

namespace InHD.dataaccess
{
    class TaxRepository
    {
        #region Fields
        List<Tax> _taxes;
        //For DB Utils
        private DBUltils _db;
        string[] _paramName;
        string[] _value;
        DBUltils.ParamType[] _paramType;
        #endregion // Fields

        #region Constructor

        public TaxRepository()
        {
            _taxes = new List<Tax>();
            this._db = new DBUltils();
            if (!_db.connectDB(Constants.Security.DefaultDBUser, Constants.Security.DefaultDBPassword))
                throw new ArgumentNullException(Constants.Messages.CannotConnectDB);
        }

        #endregion // Constructor

        public string LoadAllTaxTypes()
        {
            try
            {
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                _paramName = new string[] { };
                _paramType = new DBUltils.ParamType[] { };
                _value = new string[] { };
                DataSet ds = _db.queryDB("GetAllTax", _paramName, _paramType, _value, DBUltils.QueryType.Select);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Tax tx = Tax.CreateTaxInfo(Convert.ToString(dr["taxUNK"]), Convert.ToString(dr["taxDescription"]), Convert.ToDouble(dr["percentage"]));
                    _taxes.Add(tx);
                }
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return e.Message;
            }
            return string.Empty;
        }

        public string DeleteTax(string UNK)
        {
            bool found = false;
            foreach (Tax tax in this._taxes)
            {
                if (tax.TaxUNK.Equals(UNK))
                {
                    this._taxes.Remove(tax);
                    found = true;
                    break;
                }
            }
            if (!found)
                return "UNK is not exist";
            try
            {
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                if (UNK == null)
                    throw new ArgumentNullException("Null Unique key");
                _paramName = new string[] { "@taxUNK" };
                _paramType = new DBUltils.ParamType[] { DBUltils.ParamType.Str };
                _value = new string[] { UNK };

                _db.queryDB("DeleteTax", _paramName, _paramType, _value, DBUltils.QueryType.Delete);
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return e.Message;
            }
            //System.Windows.MessageBox.Show("Delete Tax successfully!!!");
            return string.Empty;
        }

        public List<Tax> GetAllTaxes()
        {
            return this._taxes;
        }
    }
}
